package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.util.JSONs;
import com.atguigu.gmall.product.to.ValueSkuJson;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import io.minio.messages.Item;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author lyd
* @description 针对表【spu_sale_attr(spu销售属性)】的数据库操作Service实现
* @createDate 2023-08-15 13:49:57
*/
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr>
    implements SpuSaleAttrService{

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {

      return baseMapper.getSpuSaleAttrList(spuId);
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttr(Long spuId, Long skuId) {

       return baseMapper.getSpuSaleAttr(spuId, skuId);
    }

    @Override
    public String getValueSkuJson(Long spuId) {

        List<ValueSkuJson> jsonList = baseMapper.getValueSkuJson(spuId);
        //将jsonList中的元素转换为map;一下为两种方法
        Map<String, Long> strMap = new HashMap<>();
        jsonList.forEach(item->{
            Long id = item.getId();
            String attrValueId = item.getAttrValueId();
            strMap.put(attrValueId, id);
        });
//        Map<String, Long> strMap = jsonList.stream().collect(Collectors.toMap(ValueSkuJson::getAttrValueId, ValueSkuJson::getId));
        String toJSONStr = JSONs.toJSONStr(strMap);
        return toJSONStr;
    }
}




